#!/bin/sh

# the defined variables should be:
# CONSPIRE_WRITEABLEDIR
# CONSPIRE_KEYID
# CONSPIRE_TMAX
# CONSPIRE_CLUSTERID
# CONSPIRE_DEMAND_JOB

export CONSPIRE_BROKERHOST=@DEFAULT_HOST@
export CONSPIRE_BROKERPORT=@DEFAULT_PORT@

#export FINAL_QUEUEING_SCRIPT="cd $CONSPIRE_WRITEABLEDIR; \
#if [ -n \"`which wget`\" ]; then \
#   wget http://$CONSPIRE_BROKERHOST:$CONSPIRE_BROKERPORT/hosted_files/bootstrap.sh -O bootstrap_$CONSPIRE_KEYID.sh; \
#else \
#   curl http://$CONSPIRE_BROKERHOST:$CONSPIRE_BROKERPORT/hosted_files/bootstrap.sh > bootstrap_$CONSPIRE_KEYID.sh; \
#fi; \
#   chmod +x bootstrap_$CONSPIRE_KEYID.sh; \
#   sh bootstrap_$CONSPIRE_KEYID.sh"

export FINAL_QUEUEING_SCRIPT="cd $CONSPIRE_WRITEABLEDIR; \
if [ -n \"`which wget`\" ]; then \
   wget http://$CONSPIRE_BROKERHOST:$CONSPIRE_BROKERPORT/hosted_files/python_bootstrap.sh -O bootstrap_$CONSPIRE_KEYID.sh; \
else \
   curl http://$CONSPIRE_BROKERHOST:$CONSPIRE_BROKERPORT/hosted_files/python_bootstrap.sh > bootstrap_$CONSPIRE_KEYID.sh; \
fi; \
   chmod +x bootstrap_$CONSPIRE_KEYID.sh; \
   sh bootstrap_$CONSPIRE_KEYID.sh"

if [ "$CONSPIRE_CLUSTERID" = "test" ]; then
   export JOBNAME=`echo \("$FINAL_QUEUEING_SCRIPT"\) | python @TEST_SCRIPTS_DIR@/fake_queue_test.py -V`
fi
if [ "$CONSPIRE_CLUSTERID" = "pretend" ]; then
   export JOBNAME=`echo \("$FINAL_QUEUEING_SCRIPT"\) | qsub -V`
fi
if [ "$CONSPIRE_CLUSTERID" = "mycluster" ]; then
   export JOBNAME=`echo \("$FINAL_QUEUEING_SCRIPT"\) | qsub -V`
fi
if [ "$CONSPIRE_CLUSTERID" = "bluecrystalp1" ]; then
   module add torque
   export JOBNAME=`echo \("$FINAL_QUEUEING_SCRIPT"\) | qsub -V`
fi
if [ "$CONSPIRE_CLUSTERID" = "bluecrystalp2" ]; then
   module add torque
   export JOBNAME=`echo \("$FINAL_QUEUEING_SCRIPT"\) | qsub -V`
fi
if [ "$CONSPIRE_CLUSTERID" = "emerald" ]; then
   export JOBACCEPT=`echo \("$FINAL_QUEUEING_SCRIPT"\) | bsub -e ~/err.txt -o ~/out.txt`
   export JOBNAME=`echo $JOBACCEPT | sed 's:[<>]::g' | gawk ' { print $2 } '`
fi
echo $JOBNAME
